package com.ds.lens.data.service;

import com.ds.lens.data.service.dao.LensMetricsTagsDao;
import com.ds.lens.data.service.dao.entity.LensMetricsTags;
import com.ds.lens.data.vo.in.v2.metricstags.LensMetricsTagsInVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 *
 * @author hill
 * @date 2019-11-06
 */
@Slf4j
@Service
public class LensMetricsTagsService extends BaseService{

    @Autowired
    LensMetricsTagsDao lensMetricsTagsDao;

    @Transactional
   public Boolean saveMetricsTags(LensMetricsTagsInVO lensMetricsTagsInVO)  {
        LensMetricsTags lensMetricsTags = new LensMetricsTags();
        BeanUtils.copyProperties(lensMetricsTagsInVO, lensMetricsTags);
        lensMetricsTags.setId(null);
        return true;

   }

    @Transactional
    public Boolean batchSaveMetricsTags(List<LensMetricsTags> lensMetricsTagsList)  {
        if(!CollectionUtils.isEmpty(lensMetricsTagsList)){
            lensMetricsTagsDao.batchInsert(lensMetricsTagsList);
        }

        return true;

    }


   public List<LensMetricsTags> getAllMetircsTags(){
        return lensMetricsTagsDao.selectAllMetricsTags();
   }

    public List<String> getMetricNameByAppId(Long appId){
        List<LensMetricsTags> lensMetricsTags = lensMetricsTagsDao.selectMetricNameByAppId(appId);
        List<String> metricNameList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(lensMetricsTags)){
            for (LensMetricsTags temp : lensMetricsTags){
                metricNameList.add(temp.getMetricName());
            }
        }
        return metricNameList;
    }

    public List<String> getMetricTagKeyByAppId(Long appId, String metricsName){
        List<LensMetricsTags> lensMetricsTags = lensMetricsTagsDao.selectMetricTagKeyByAppId(appId,metricsName);
        List<String> tagKeyList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(lensMetricsTags)){
            for (LensMetricsTags temp : lensMetricsTags){
                if(temp != null && !StringUtils.isEmpty(temp.getTagKey())){
                    tagKeyList.add(temp.getTagKey());
                }
            }
        }
        return tagKeyList;
    }

    public List<String> getMetricTagKeyValueByAppId(Long appId, String metricsName){
        List<LensMetricsTags> lensMetricsTags = lensMetricsTagsDao.selectMetricTagKeyValueByAppId(appId, metricsName);
        List<String> tagValueList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(lensMetricsTags)){
            for (LensMetricsTags temp : lensMetricsTags){
                if(temp != null && !StringUtils.isEmpty(temp.getTagFull())){
                    tagValueList.add(temp.getTagFull());
                }

            }
        }
        return tagValueList;
    }






}
